package jers.impl;

import java.io.IOException;
import java.rmi.MarshalledObject;
import java.rmi.RemoteException;
import java.rmi.activation.Activatable;
import java.rmi.activation.ActivationID;
import java.rmi.server.UnicastRemoteObject;

import jers.Callback;
import jers.Job;
import jers.Result;
import jers.Service;
import jers.util.Utility;


/**
 * Classe que implementa a interface {@link Service}.
 * <p>
 * Esta extende a classe {@link UnicastRemoteObject} para prover 
 * os servicos remotos via RMI.
 * </p>
 * 
 * @see {@link UnicastRemoteObject}, {@link SystemFactory}, {@link Service}
 * 
 * @author 
 * 		<a href="mailto:felipe.zappala@gmail.com">
 * 			Luis Felipe Zappala
 * 		</a> [ <a href="http://lncc.br/ist">ISTCC-P</a> / 2009 ]
 */
final class RemoteActivableService extends Activatable implements Service {

	/** O ID serial da versao da classe */
	private static final long serialVersionUID = 5657308306634053384L;

	/** O servico {@link Service} delegado a executar as tarefas concretas */
	private final Service delegateService;
	
	protected static RemoteActivableService getInstance() {
		try {
			return new RemoteActivableService(null, null);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	private RemoteActivableService(ActivationID id, 
			@SuppressWarnings("unused") MarshalledObject<Service> data) 
			throws IOException {
		super(id, 0);
		this.delegateService = new RemoteService();
	}

	public void dispatch(Job job, Callback callback) throws RemoteException {
		this.delegateService.dispatch(job, callback);
	}

	public Result execute(Job job) throws RemoteException {
		return this.delegateService.execute(job);
	}

	/**
	 * Retorna uma representacao {@link String} do objeto.
	 * <p>
	 * <b>Formato:</b><br>
	 * 		<code>ClassName{attributeA:valueA,...,attributeN:valueN}</code>
	 * </p>
	 * @return 
	 * 		A representacao {@link String} do objeto.
	 */
	@Override
	public String toString() {
    	return Utility.toString(this);
    }

}
